#!/bin/bash
# This script is released under the GNU General Public License 3.0
# Check the COPYING file included with this distribution

# bootloader_mainmenu
# main menu for bootloader installation
#
# parameters (required)
#  CONFIG_LIST: One string items of defined FSspec
#
# returns 0 on success
# returns $ERROR_CANCEL=64 on user cancel
# anything else is a real error
# reason: show_dialog() needs a way to exit "Cancel"
#
# writes menus and noise to STDERR

# location of other scripts to source
readonly SHAREDIR="$(dirname ${0})" || exit $?

# source common variables, functions and error handling
source "${SHAREDIR}"/common.sh || exit $?

#########################################
## START: dialog functions/definitions ##

## END: dialog functions/definitions ##
#######################################

#####################
## begin execution ##

# check input
check_num_args "$(basename $0)" 1 $# || exit $?
CONFIG_LIST="${1}"
SELECTION=
RETSUB=

# mount everything, including cryptsetup
"${SHAREDIR}"/FSspec mountall "${CONFIG_LIST}" || exit $?

# TODO: enable uefi options once they are implemented
# SELECTION="$(show_dialog --menu "Which bootloader would you like to use? Grub is the Pentoo default." \
	# 0 0 4 \
	# "GRUB" "Use the GRUB bootloader (default)" \
	# "UEFI-GRUB" "Use GRUB2 and UEFI (unsupported)" \
	# "UEFI" "Boot kernel directly by UEFI (unsupported)" \
	# "None" "Warning: you must install your own bootloader!")" \
	# || exit $?
SELECTION="$(show_dialog --menu "Which bootloader would you like to use? Grub is the Pentoo default." \
	0 0 4 \
	"GRUB" "Use the GRUB bootloader (default)" \
	"None" "Warning: you must install your own bootloader!")" \
	|| exit $?
case "${SELECTION}" in
	"GRUB")
		"${SHAREDIR}"/bootloader_grub "${CONFIG_LIST}" || exit $?
		;;
	"UEFI-GRUB")
		"${SHAREDIR}"/bootloader_uefigrub "${CONFIG_LIST}" || exit $?
		;;
	"UEFI")
		"${SHAREDIR}"/bootloader_uefi "${CONFIG_LIST}" || exit $?
		;;
	"None")
		# TODO: Give user info about kernel params and such
		;;
esac

exit 0
